package com.sunday.android.fangdaifu.bukong.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sunday.android.fangdaifu.bukong.BaseApp;
import com.sunday.android.fangdaifu.bukong.MainActivity;
import com.sunday.android.fangdaifu.bukong.R;
import com.sunday.android.fangdaifu.bukong.entity.Member;
import com.sunday.android.fangdaifu.bukong.entity.event.ResetPwd;
import com.sunday.android.fangdaifu.bukong.http.ApiClient;
import com.sunday.android.fangdaifu.bukong.utils.BaseUtil;
import com.sunday.android.fangdaifu.bukong.utils.Constants;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by yun_sheng on 2017/6/28.
 */

public class LoginActivity extends BaseActivity{
    @Bind(R.id.edt_username)
    EditText edtUsername;
    @Bind(R.id.edt_password)
    EditText edtPassword;
    @Bind(R.id.see)
    TextView see;
    @Bind(R.id.btn_login)
    TextView btnLogin;
    @Bind(R.id.forget_pwd)
    TextView forgetPwd;
    private String usernName;
    private String userPwd;
    private Context mContext;
    private boolean isSee = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.acitivity_login);
        ButterKnife.bind(this);
        mContext=this;
        boolean isLogin = SharePerferenceUtils.getIns(mContext).getBoolean(Constants.IS_LOGIN, false);
        if (isLogin){
            BaseUtil.openActivity(mContext,MainActivity.class);
            finish();
        }
    }
    @OnClick({R.id.btn_login, R.id.forget_pwd, R.id.see})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                usernName = edtUsername.getText().toString().trim();
                userPwd = edtPassword.getText().toString().trim();
                if (TextUtils.isEmpty(usernName)||TextUtils.isEmpty(userPwd)){
                    ToastUtils.showToast(mContext,"请输入用户名和密码");
                    return;
                }
                showLoadingDialog(0);
                login(Constants.USER_TYPE,usernName,userPwd);
                break;
            case R.id.forget_pwd:
                BaseUtil.openActivity(mContext, ForgetPwdActivity.class);
                break;
            case R.id.see:
                see.setSelected(!isSee);
                isSee = !isSee;
                if (isSee) {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                } else {
                    edtPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
                }
                break;
        }
    }

    private void login(int type, String name, String password) {

        Call<ResultDO<Member>> call = ApiClient.getApiAdapter().AaLogin(type, name, password);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0 && response.body().getResult() != null) {
                    dissMissDialog();
                    SharePerferenceUtils.getIns(mContext).saveOAuth(response.body().getResult());
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_FIRST,false);
                    BaseApp.getInstance().setMemberId(response.body().getResult().getId());
                    BaseUtil.openActivity(mContext, MainActivity.class);
                    finish();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                    dissMissDialog();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }

    public void onEvent(ResetPwd resetPwd){
        finish();
    }

}
